package bkone;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class addblog
 */
@WebServlet("/addblog")
public class addblog extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	public static List<Map<String, String>> remoteUserInfos = new ArrayList<>();
	private static final String JDBC_URL = "jdbc:mysql://localhost:3309/blog?useUnicode=true&characterEncoding=utf8";
	private static final String DB_USER = "root";
	private static final String DB_PWD = "ZLWL";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addblog() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("prompt: ").append(request.getContextPath());
	} 

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
		String namea = (String)request.getSession().getAttribute("namet");
		System.out.println("addblog:" + namea);
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			e.printStackTrace();
		}
		Connection conn;
		try {
			conn = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PWD);
			Statement stmt = conn.createStatement();
			String sqla = "select id from user_zc where name = '" + namea + "'";
			ResultSet rs = stmt.executeQuery(sqla);
			System.out.println("addblog:" + rs);
			String setid = "";
			while (rs.next()) {
				String getid = rs.getString("id");
			    System.out.println("addblog:" + getid);
			    setid = getid;
			}
			System.out.println("addblog:" + setid);
			SimpleDateFormat sdfFull1 = new SimpleDateFormat("yyyy-MM-dd ");
			SimpleDateFormat sdfFull2 = new SimpleDateFormat(":mm:ss");
			Calendar cld = Calendar.getInstance();
			Date currentDate = new Date();
			System.out.println("完整的(前)：" + sdfFull1.format(currentDate));
			System.out.println("完整的(后)：" + sdfFull2.format(currentDate));
			System.out.println("当前天,时间：" + cld.get(Calendar.HOUR_OF_DAY));
			String now_time = sdfFull1.format(currentDate) + cld.get(Calendar.HOUR_OF_DAY) + sdfFull2.format(currentDate);
			System.out.println("当前时间：" + now_time);
			String titles = request.getParameter("title");
			String contents = request.getParameter("content");
			String tydis = request.getParameter("test2");
	        System.out.println("titles:" + titles);
	        System.out.println("contents:" + contents);
	        System.out.println("tydis1:" + tydis);
	        if(tydis == ""){
	        	tydis = "0";
	        }
	        System.out.println("tydis2:" + tydis);
	        String sqltypeshul = "select id from blog_type order by id desc limit 1";
	        ResultSet rstypeshul = stmt.executeQuery(sqltypeshul);
	        int settypeshul = 0;
			while (rstypeshul.next()) {
				int gettypeshul = rstypeshul.getInt(1);
			    settypeshul = gettypeshul;
			}
			System.out.println(settypeshul);
			boolean isifcunz = false;
			int ifvsv = 1;
			while(ifvsv <= settypeshul){
				if(ifvsv == Integer.parseInt(tydis)){
					isifcunz = true;
				}
				System.out.println(ifvsv == Integer.parseInt(tydis));
				ifvsv ++;
			}
			System.out.println("isifcunz:" + isifcunz);
	        if(isifcunz){
	        String blogluru = "insert into blog (title,author_id,publish_time,read_count,comment_count,content,type_id) values(?,?,?,?,?,?,?)";
			PreparedStatement pstmt = conn.prepareStatement(blogluru);
			pstmt.setString(1, titles);
			pstmt.setString(2, setid);
			pstmt.setString(3, now_time);
			pstmt.setString(4, "0");
			pstmt.setString(5, "0");
			pstmt.setString(6, contents);
			pstmt.setString(7, tydis);
			int insertResult = 0;
			if (!titles.equals("")) {
				if (!contents.equals("")) {
		    	    insertResult = pstmt.executeUpdate();
		    	    if (insertResult > 0) {
		    	    	response.getWriter().write("{'ok':true}");
		    	    } else {
		    	    	response.getWriter().write("{'ok':false}");
		    	    }
			    }  else{
			    	response.getWriter().write("content can't be blank");
			    }
	        } else{
		    	response.getWriter().write("title can't be blank");
		    }
			pstmt.close();
	        } else {
	        	response.getWriter().write("please select a type of blog");
	        }
	        rstypeshul.close();
			rs.close();
		    stmt.close();
		    conn.close();
		} catch (SQLException e) {
		    e.printStackTrace();
		}
	}
}
